package com.yingpai.fitness.dvp_2_0.main.activity.mine;

import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.ShopDetailActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.InviteBean;
import com.yingpai.fitness.dvp_2_0.main.util.DialogUtil;
import com.yingpai.fitness.dvp_2_0.main.util.WXShareUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/12/29.
 */

public class InviteFriendActvity extends CommonActivity {
    @BindView(R.id.invite_num)
    TextView inviteNum;
    @BindView(R.id.count_num)
    TextView countNum;
    @BindView(R.id.rule)
    TextView rule;
    @BindView(R.id.rule_title)
    TextView rule_title;
    private InviteBean mInviteBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_invite_friend);
    }

    @Override
    protected void initView() {
        setTitle("邀请好友");
    }

    @Override
    protected void initData() {
        showDialog(this,null);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.INVITE_FRIEND, null, Url.INVITE_FRIEND, new Callback<InviteBean>() {
            @Override
            public void onSuccess(InviteBean inviteBean) {
                if(inviteBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                    mInviteBean = inviteBean;
                    inviteNum.setText(inviteBean.getData().getInviteCode());
                    countNum.setText(inviteBean.getData().getCount()+"");
                    rule_title.setText(inviteBean.getData().getInviteRule().getTitle());
                    rule.setText(inviteBean.getData().getInviteRule().getInfo());
                }else{
                    showToast(inviteBean.getMessage()+"");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({R.id.copy, R.id.invite_tv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.copy:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(inviteNum.getText().toString());
                showToast("复制成功，可以发给朋友们了");
                break;
            case R.id.invite_tv:
                DialogUtil.getInstance().showBottomDialog(this,R.layout.dialog_invite_share);
                LinearLayout share_dialog = (LinearLayout) DialogUtil.getInstance().dialog_bottom_layout;
                share_dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.getInstance().dialog_bottom.dismiss();
                    }
                });
                share_dialog.findViewById(R.id.wechat_friend_share).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.getInstance().dialog_bottom.dismiss();
                        WXShareUtil wxShareUtil = new WXShareUtil(InviteFriendActvity.this);
                        wxShareUtil.registerWx();
                        //防止取值失败
                        try {
                            wxShareUtil.share(mInviteBean.getData().getUrl(),
                                    mInviteBean.getData().getTitle(),
                                    mInviteBean.getData().getDescription(),
                                    mInviteBean.getData().getPhotoUrl(), wxShareUtil.SHARETOFRIEND);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                share_dialog.findViewById(R.id.wechat_circle_share).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.getInstance().dialog_bottom.dismiss();
                        WXShareUtil wxShareUtil = new WXShareUtil(InviteFriendActvity.this);
                        wxShareUtil.registerWx();
                        //防止取值失败
                        try {
                            wxShareUtil.share(mInviteBean.getData().getUrl(),
                                    mInviteBean.getData().getTitle(),
                                    mInviteBean.getData().getDescription(),
                                    mInviteBean.getData().getPhotoUrl(), wxShareUtil.SHARETOPYQ);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
    }
}
